package com.zut.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zut.base.common.entry.ResultVO;
import com.zut.base.common.util.StringUtils;
import com.zut.base.entity.Directory;
import com.zut.base.entity.Project;
import com.zut.base.service.IDirectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 目录表 前端控制器
 * </p>
 *
 * @author yyl
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/imp/directory")
public class DirectoryController {

    @Autowired
    private IDirectoryService directoryServiceImpl;

    //添加目录
    @PostMapping("/addDirectory")
    public Object addDirectory(@RequestBody Directory directory) {
        boolean rs = directoryServiceImpl.save(directory);
        if (rs) return "添加成功";
        return new ResultVO<>(-9999, "添加失败");
    }

    //批量添加目录（接口）
    @PostMapping("/addBatchDir")
    public Object addBatchDir(@RequestBody List<Directory> directory) {
        boolean rs = directoryServiceImpl.saveBatch(directory);
        if (rs) return "添加成功";
        return new ResultVO<>(-9999, "添加失败");
    }

    //目录列表查询(项目一级目录)
    @GetMapping("/queryDirListLevOne")
    public Object queryDirListLevOne(@NotNull String id, @NotNull String category, String projectId) {
        QueryWrapper<Directory> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(projectId))
            wrapper.eq("projectid", projectId);
        wrapper.eq("createBy", id).eq("category", category).eq("parentId", "-1");
        //根据用户id查询用户创建的一级目录
        List<Directory> list = directoryServiceImpl.list(wrapper);
        if (!StringUtils.isEmpty(projectId)) return list;
        //查询用户加入项目所在的一级目录
        List<Directory> dirListByUserId = directoryServiceImpl.getDirListByUserId(id);
        if (dirListByUserId != null) list.addAll(dirListByUserId);
        if (list.size() == 0) return list;
        Set<Directory> set = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
        set.addAll(list);
        //过滤二级目录
        for (Iterator<Directory> it = set.iterator(); it.hasNext(); ) {
            if (!"-1".equals(it.next().getParentid())) {
                it.remove();
            }
        }
        return set;
    }


    //目录查询（项目二级目录）
    @GetMapping("/queryDirListLevSecond")
    public Object queryDirListLevSecond(@NotNull String dirId, @NotNull String userId) {
        QueryWrapper<Directory> wrapper = new QueryWrapper<>();
        wrapper.eq("createBy", userId).eq("category", 2).eq("parentId", dirId);
        //根据用户id查询用户创建的二级目录
        List<Directory> list = directoryServiceImpl.list(wrapper);
        //查询用户加入项目所在的二级目录
        List<Directory> dirListByUserId = directoryServiceImpl.getDirListSec(userId, dirId);
        if (dirListByUserId != null) list.addAll(dirListByUserId);
        if (list.size() == 0) return list;
        Set<Directory> set = new TreeSet<>((o1, o2) -> o1.getId().compareTo(o2.getId()));
        set.addAll(list);
        return set;
    }

    //目录查询（模块二级目录）
    @GetMapping("/queryModuleListSecond")
    public Object queryModuleListSecond(@NotNull String id) {
        QueryWrapper<Directory> wrapper = new QueryWrapper<>();
        wrapper.eq("parentid", id);
        List<Directory> list = directoryServiceImpl.list(wrapper);
        return list;
    }
}
